package com.x.bff.app.service.customer;

import com.x.bff.app.model.vo.customer.*;
import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.enums.CustomerResourceOperBizScene;
import com.x.provider.api.customer.enums.CustomerVerificationTypeEnum;
import com.x.provider.api.customer.model.dto.customer.*;
import com.x.provider.api.customer.service.CustomerRpcService;
import com.x.provider.api.general.service.CmsRpcService;
import com.x.provider.api.oss.model.dto.face.IdCardVerificationReqDTO;
import com.x.provider.api.oss.service.FaceIdRpcService;
import com.x.provider.api.oss.service.OssRpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService{

    private final CustomerRpcService customerRpcService;
    private final OssRpcService ossRpcService;
    private final CmsRpcService cmsRpcService;
    private final FaceIdRpcService faceIdRpcService;

    public CustomerServiceImpl(CustomerRpcService customerRpcService,
                               OssRpcService ossRpcService,
                               CmsRpcService cmsRpcService,
                               FaceIdRpcService faceIdRpcService){
        this.customerRpcService = customerRpcService;
        this.ossRpcService = ossRpcService;
        this.cmsRpcService = cmsRpcService;
        this.faceIdRpcService = faceIdRpcService;
    }

    @Override
    public String loginExternal(LoginExternalReqVO loginExternalReq) {
        LoginExternalReqDTO loginExternalReqDTO = BeanUtil.prepare(loginExternalReq, LoginExternalReqDTO.class);
        return customerRpcService.loginExternal(loginExternalReqDTO).getData();
    }

    @Override
    public void logout(String token) {
        customerRpcService.logout(token);
    }

    @Override
    public void customerUserNameUpdate(Long customerId, CustomerUserNameUpdateReqVO customerUserNameUpdateReq) {
        CustomerUserNameUpdateReqDTO customerUserNameUpdateDTO = BeanUtil.prepare(customerUserNameUpdateReq, CustomerUserNameUpdateReqDTO.class);
        customerUserNameUpdateDTO.setCustomerId(customerId);
        customerRpcService.customerUserNameUpdate(customerUserNameUpdateDTO);
    }

    @Override
    public SaveResumeVO saveResume(Long customerId, CustomerResumeSaveReqVO customerResumeSaveReq) {
        CustomerResumeSaveReqDTO customerResumeSaveReqDTO = BeanUtil.prepare(customerResumeSaveReq, CustomerResumeSaveReqDTO.class);
        customerResumeSaveReqDTO.setCustomerId(customerId);
        CustomerDTO customer = customerRpcService.listCustomer(CustomerListReqDTO.builder().customerIds(Arrays.asList(customerId)).sessionCustomerId(0L)
                .customerOptions(Arrays.asList(CustomerOptions.CUSTOMER_RESUME.name())).build()).getData().get(customerId);
        Boolean addResource = customer.getCustomerResumeList().size() > 0 && customer.getCustomerResumeList().get(0).getId() != null ? false : true;
        Long resumeId = customerRpcService.saveCustomerResume(customerResumeSaveReqDTO).getData();
        SaveResumeVO result = SaveResumeVO.builder().id(resumeId).build();
        if (addResource){
            CustomerResourceOperBizScene.CustomerResourceAddBizSceneEnum bizScene = CustomerResourceOperBizScene.CustomerResourceAddBizSceneEnum.ADD_RESUME_READ_COUNT_4_RESUME_CREATED;
            customerRpcService.operateCustomerResource(CustomerResourceOperReqDTO.builder().operBizSceneId(bizScene.getId()).customerId(customerId).build());
            result.setSaveResumeRewardResourceCount(bizScene.getCount());
        }
        return result;
    }

    @Override
    public void saveResumeAttribute(CustomerResumeAttributeSaveReqVO customerResumeAttributeSaveReq) {
        CustomerResumeAttributeSaveReqDTO reqDTO = BeanUtil.prepare(customerResumeAttributeSaveReq, CustomerResumeAttributeSaveReqDTO.class);
        log.info("save resume attribute, key:{} value:{}", reqDTO.getKey(), reqDTO.getValue());
        customerRpcService.saveCustomerResumeAttribute(reqDTO);
    }

    @Override
    public void customerIdCardVerification(Long customerId, String idCard, String name){
        CustomerDTO customerDTO = customerRpcService.listCustomer(CustomerListReqDTO.builder().customerIds(Arrays.asList(customerId))
                .customerOptions(Arrays.asList(CustomerOptions.VERIFICATION.name())).build()).getData().get(customerId);
        if (customerDTO.getVerificationList().stream().filter(item -> item.getPass() && item.getVerificationType().equals(CustomerVerificationTypeEnum.ID_CARD.getValue())).findFirst().isPresent()){
            return;
        }
        faceIdRpcService.idCardVerification(IdCardVerificationReqDTO.builder().idCard(idCard).name(name).build());
        customerRpcService.saveCustomerVerification(CustomerVerificationSaveReqDTO.builder().customerId(customerId).pass(true)
                .verificationType(CustomerVerificationTypeEnum.ID_CARD.getValue()).build());

    }
}
